<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 * 
 */

/**
 * Top view of all available document libraries of a site
 * 
 * @author Bendsoft
 */
class SharePointDocumentLibraries extends CamelotExceptionHandling {

    /**
     * Reference to the settingsarray with connections
     * @var string
     */
    private $connection_name;

    /**
     * (optional) Include hidden document libraries or not, defaults to true
     * @var bool $includeHidden  
     */
    private $includeHidden;

    /**
     * Property to store the CamelotDocumentLibrariesObject
     * @var array
     */
    public $CamelotDocumentLibrariesObjects;

    /**
     * Construct the class instance
     * @param string $connection_name Which connection to use from the settings
     * @param bool $includeHidden (optional) Include hidden document libraries or not, defaults to true
     */
    public function __construct($connection_name, $includeHidden = true) {
        $this->connection_name = $connection_name;
        $this->includeHidden = $includeHidden;
        $this->CamelotDocumentLibrariesObjects = new ArrayObject();

        $this->fill();
    }

    /**
     * Fill the CamelotDocumentLibrariesObject with data
     * @return void
     */
    public function fill() {

        $command = new SharePointSqlCommand();
        $command->connection_name = $this->connection_name;
        $command->sql = "SHOW TABLES";

        $GetLibraries = new SharePointQuery($command);

        foreach ($GetLibraries->CamelotSoap->_sorted->_content as $library) {
            $library = (object) $library;

            // If only include non-hidden
            if ($this->includeHidden === false && $library->Hidden == 'true') {
                continue;
            }

            // If not BaseType 1
            if ($library->BaseType != 1) {
                continue;
            }

            $name = $library->Name;
            $title = $library->Title;
            $hidden = $library->Hidden;
            $baseType = $library->BaseType;
            $itemCount = $library->ItemCount;
            $defaultViewUrl = $this->getDefaultViewUrl($library->DefaultViewUrl);

            // Add to the ArrayObject
            $camelotDocumentLibrariesObject = new CamelotDocumentLibrariesObject($name, $title, $hidden, $baseType, $itemCount, $defaultViewUrl);
            $this->CamelotDocumentLibrariesObjects->append($camelotDocumentLibrariesObject);
        }
        
        $this->commandStatus = true;
    }

    /**
     * Returns only first part of the path
     * "/Shared Documents/Forms/AllItems.aspx" would return "Shared Documents"
     * @param string $defaultViewUrl 
     * @return string
     */
    private function getDefaultViewUrl($defaultViewUrl) {
        $viewUrl = explode("/", $defaultViewUrl);
        return (count($viewUrl) > 1) ? $viewUrl[1] : $defaultViewUrl;
    }

}

/**
 * Default "SHOW TABLES" object
 * 
 * @author Bendsoft
 */
class CamelotDocumentLibrariesObject {

    /**
     *
     * @var string
     */
    public $name;

    /**
     *
     * @var string
     */
    public $title;

    /**
     *
     * @var bool
     */
    public $hidden;

    /**
     * 1 = Document Library 
     * @var int
     */
    public $baseType;

    /**
     * No items in the library
     * @var int
     */
    public $itemCount;

    /**
     * We need this to verify the document library path
     * @var string
     */
    public $defaultViewUrl;

    /**
     *
     * @param string $name
     * @param string $title
     * @param bool $hidden
     * @param int $baseType
     * @param int $itemCount
     * @param string $defaultViewUrl 
     */
    public function __construct($name, $title, $hidden, $baseType, $itemCount, $defaultViewUrl) {
        $this->name = $name;
        $this->title = $title;
        $this->hidden = $hidden;
        $this->baseType = $baseType;
        $this->itemCount = $itemCount;
        $this->defaultViewUrl = $defaultViewUrl;
    }

}

?>
